{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "source": [
    "import tensorflow as tf"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "source": [
    "in1 = tf.random.normal([2,100])\r\n",
    "in1.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 100])"
      ]
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "r_in1 = tf.reshape(in1,(2,1,1,100))"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "source": [
    "# conv2D是 ceil((W-F+1)/S),  \r\n",
    "#  cW = ceil((W-F+1)/S)\r\n",
    "#  cW*S-1 = W-F+1\r\n",
    "#  W = cW*s+F-2\r\n",
    "#  \r\n",
    "conv1 = tf.keras.layers.Conv2DTranspose(64*8,4,1,'valid',use_bias=False)(r_in1)\r\n",
    "# (1+4-1)*1 = 4\r\n",
    "conv1.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 4, 4, 512])"
      ]
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "source": [
    "_conv1 = tf.keras.layers.Conv2DTranspose(64*8,4,1,'same',use_bias=False)(r_in1)\r\n",
    "_conv1.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 1, 1, 512])"
      ]
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "source": [
    "x = tf.random.normal((2,7,7,1))\r\n",
    "x.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 7, 7, 1])"
      ]
     },
     "metadata": {},
     "execution_count": 43
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "source": [
    "c1 = tf.keras.layers.Conv2D(32,3,1,'valid',use_bias=False)(x)\r\n",
    "c1.shape "
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 5, 5, 32])"
      ]
     },
     "metadata": {},
     "execution_count": 45
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "source": [
    "c2 = tf.keras.layers.Conv2D(32,3,2,'valid',use_bias=False)(x)\r\n",
    "c2.shape "
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 3, 3, 32])"
      ]
     },
     "metadata": {},
     "execution_count": 46
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "source": [
    "cx0 = tf.keras.layers.Conv2D(4,kernel_size=3,strides=2,padding='same',use_bias=False)(x)\r\n",
    "cx0.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 4, 4, 4])"
      ]
     },
     "metadata": {},
     "execution_count": 36
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "source": [
    "x0_ = tf.keras.layers.Conv2DTranspose(64*8,4,1,'same',use_bias=False)(x)\r\n",
    "x0_.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 7, 7, 512])"
      ]
     },
     "metadata": {},
     "execution_count": 38
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "source": [
    "x0 = tf.keras.layers.Conv2DTranspose(64*8,4,3,'same',use_bias=False)(cx0)\r\n",
    "x0.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 12, 12, 512])"
      ]
     },
     "metadata": {},
     "execution_count": 37
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "source": [
    "x1 = tf.keras.layers.Conv2DTranspose(64*8,3,1,'valid',use_bias=False)(x)\r\n",
    "#  W = W`*s+F-2 = 7*1+4-2 = 9\r\n",
    "#  W = W`*s+F-2 = 7*1+4-1 = 10\r\n",
    "x1.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 9, 9, 512])"
      ]
     },
     "metadata": {},
     "execution_count": 47
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "source": [
    "x1_ = tf.keras.layers.Conv2D(4,4,1,'valid',use_bias=False)(x)\r\n",
    "x1_.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 4, 4, 4])"
      ]
     },
     "metadata": {},
     "execution_count": 39
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "source": [
    "x2 = tf.keras.layers.Conv2DTranspose(64*8,3,2,'valid',use_bias=False)(x)\r\n",
    "\r\n",
    "# W = W`*s+F-2 = 7*2+3-2 = 15\r\n",
    "x2.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 15, 15, 512])"
      ]
     },
     "metadata": {},
     "execution_count": 16
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "source": [
    "x3 = tf.keras.layers.Conv2DTranspose(64*8,4,2,'valid',use_bias=False)(x)\r\n",
    "\r\n",
    "#  W = W`*s+F-2 = 7*2+4-2 = 16\r\n",
    "x3.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 16, 16, 512])"
      ]
     },
     "metadata": {},
     "execution_count": 17
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "source": [
    "x4 = tf.keras.layers.Conv2DTranspose(64*8,4,3,'valid',use_bias=False)(x)\r\n",
    "#  W = W`*s+F-2 = 7*3+4-2 = 23\r\n",
    "#  W = W`*s+F-2 = 7*3+4-3 = 22\r\n",
    "x4.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 22, 22, 512])"
      ]
     },
     "metadata": {},
     "execution_count": 18
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "source": [
    "x5 = tf.keras.layers.Conv2DTranspose(64*8,4,4,'valid',use_bias=False)(x)\r\n",
    "#  W = W`*S+F-S = 7*4+4-4 = 28\r\n",
    "x5.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 28, 28, 512])"
      ]
     },
     "metadata": {},
     "execution_count": 19
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 上面是 Conv2DTranspose的 valid与same 格式区别\r\n",
    "# 下面计算 Conv2D的valid与same"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "source": [
    "in2 = tf.random.normal((2,256,256,3))\r\n",
    "in2.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 256, 256, 3])"
      ]
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "source": [
    "# 公式 ceil((W-F+1)/S) : W:输入宽高尺寸,F:filter的尺寸,kernel_size,S:步长\r\n",
    "c2 = tf.keras.layers.Conv2D(32,3,1,'valid',use_bias=False)(in2)\r\n",
    "# 高(row): (256-3+1)/1 = 254, 宽(column)也一样 \r\n",
    "c2.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 254, 254, 32])"
      ]
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "source": [
    "tx = tf.random.normal((2,25,25,3))\r\n",
    "tx.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 25, 25, 3])"
      ]
     },
     "metadata": {},
     "execution_count": 24
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "source": [
    "ctx = tf.keras.layers.Conv2DTranspose(32,3,2,padding='same',use_bias=False)(tx)\r\n",
    "ctx.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 50, 50, 32])"
      ]
     },
     "metadata": {},
     "execution_count": 26
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "source": [
    "tctx = tf.keras.layers.Conv2D(32,3,2,'same',use_bias=False)(tx)\r\n",
    "tctx.shape "
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 13, 13, 32])"
      ]
     },
     "metadata": {},
     "execution_count": 28
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "source": [
    "tx1 = tf.random.normal((2,24,24,3))\r\n",
    "tx1.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 24, 24, 3])"
      ]
     },
     "metadata": {},
     "execution_count": 29
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "source": [
    "tctx1 = tf.keras.layers.Conv2D(32,3,2,'same',use_bias=False)(tx1)\r\n",
    "tctx1.shape "
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 12, 12, 32])"
      ]
     },
     "metadata": {},
     "execution_count": 30
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "source": [
    "tctx2 = tf.keras.layers.Conv2D(32,3,5,'same',use_bias=False)(tx1)\r\n",
    "tctx2.shape "
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 5, 5, 32])"
      ]
     },
     "metadata": {},
     "execution_count": 34
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "source": [
    "tctx = tf.keras.layers.Conv2D(32,3,2,'same',use_bias=False)(tx)\r\n",
    "tctx.shape "
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 13, 13, 32])"
      ]
     },
     "metadata": {},
     "execution_count": 31
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "source": [
    "rtctx = tf.keras.layers.Conv2DTranspose(32,3,2,'same',use_bias=False)(tctx)\r\n",
    "rtctx.shape "
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([2, 26, 26, 32])"
      ]
     },
     "metadata": {},
     "execution_count": 35
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "source": [
    "x_shape = (128,256,256,3)\r\n",
    "xc = tf.random.normal(x_shape)\r\n",
    "xc.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([128, 256, 256, 3])"
      ]
     },
     "metadata": {},
     "execution_count": 23
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "source": [
    "c2d_x = tf.keras.layers.Conv2D(32,kernel_size=3,strides=2,padding='same')(xc)\r\n",
    "#  (W-F+1)/S = (256-3+1)/2 = \r\n",
    "c2d_x.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([128, 128, 128, 32])"
      ]
     },
     "metadata": {},
     "execution_count": 22
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "source": [
    "c2d_xx = tf.keras.layers.Conv2D(32,kernel_size=3,strides=2,padding='same',input_shape=x_shape[1:])(x)\r\n",
    "c2d_xx.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([128, 128, 128, 32])"
      ]
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "source": [
    "c2d_x_default_padding = tf.keras.layers.Conv2D(32,kernel_size=3,strides=2,input_shape=x_shape[1:])(x)\r\n",
    "c2d_x_default_padding.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([128, 127, 127, 32])"
      ]
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "c2d_x_default_padding_strides = tf.keras.layers.Conv2D(32,kernel_size=3,input_shape=x_shape[1:])(x)\r\n",
    "c2d_x_default_padding_strides.shape"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "c2d_x.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([128, 128, 128, 32])"
      ]
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "source": [
    "c2d_x1 = tf.keras.layers.Conv2D(32,kernel_size=3,strides=1,padding=\"same\")(x)\r\n",
    "c2d_x1.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([128, 256, 256, 32])"
      ]
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "source": [
    "input_shape = (4, 28, 28, 3)\r\n",
    "x = tf.random.normal(input_shape)\r\n",
    "y = tf.keras.layers.Conv2D(\r\n",
    "2, 3, activation='relu', input_shape=input_shape[1:])(x)\r\n",
    "print(y.shape)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "(4, 26, 26, 2)\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "source": [
    "data_shape = (10,200,200,3)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "source": [
    "x = tf.random.normal(data_shape)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "source": [
    "y = tf.keras.layers.Conv2D(input_shape=data_shape[1:],filters=32, kernel_size=3, strides=(2, 2), activation='relu')(x)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "source": [
    "y.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([10, 99, 99, 32])"
      ]
     },
     "metadata": {},
     "execution_count": 18
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "source": [
    "y1 = tf.keras.layers.Conv2D(input_shape=data_shape[1:],filters=32, kernel_size=3, strides=(3, 3), activation='relu')(x)\r\n",
    "y1.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([10, 66, 66, 32])"
      ]
     },
     "metadata": {},
     "execution_count": 19
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "ix_shape = (10,7,7,32)\r\n",
    "ix = tf.random.normal(ix_shape)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "cy = tf.keras.layers.Conv2DTranspose(\r\n",
    "    input_shape=(7,7,32),\r\n",
    "    filters=64,\r\n",
    "    kernel_size=3,\r\n",
    "    strides=(2,2),\r\n",
    "    padding='SAME',\r\n",
    "    activation=tf.nn.relu\r\n",
    ")(ix)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "cy.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([10, 14, 14, 64])"
      ]
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "source": [
    "c2 = tf.keras.layers.Conv2DTranspose(\r\n",
    "    input_shape=ix_shape[1:],\r\n",
    "    filters=128,\r\n",
    "    kernel_size=(5,5),\r\n",
    "    strides=(1,1),\r\n",
    "    padding='same',\r\n",
    "    use_bias=False\r\n",
    ")(ix)\r\n",
    "c2.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([10, 7, 7, 128])"
      ]
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "source": [
    "cy1 = tf.keras.layers.Conv2DTranspose(\r\n",
    "    input_shape=(7,7,32),\r\n",
    "    filters=64,\r\n",
    "    kernel_size=3,\r\n",
    "    padding='SAME',\r\n",
    "    activation=tf.nn.relu\r\n",
    ")(ix)\r\n",
    "\r\n",
    "cy1.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([10, 7, 7, 64])"
      ]
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "source": [
    "cy2 = tf.keras.layers.Conv2DTranspose(\r\n",
    "    input_shape=(7,7,32),\r\n",
    "    filters=32,\r\n",
    "    kernel_size=3,\r\n",
    "    padding='SAME',\r\n",
    "    activation=tf.nn.relu\r\n",
    ")(ix)\r\n",
    "\r\n",
    "cy2.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([10, 7, 7, 32])"
      ]
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "source": [
    "cy3 = tf.keras.layers.Conv2DTranspose(\r\n",
    "    input_shape=(7,7,32),\r\n",
    "    filters=64,\r\n",
    "    kernel_size=3,\r\n",
    "    strides=(3,3),\r\n",
    "    padding='SAME',\r\n",
    "    activation=tf.nn.relu\r\n",
    ")(ix)\r\n",
    "cy3.shape"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "TensorShape([10, 21, 21, 64])"
      ]
     },
     "metadata": {},
     "execution_count": 16
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.7.1",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.7.1 64-bit ('KerasEnv': virtualenv)"
  },
  "interpreter": {
   "hash": "49709926ca079d7fc3208f0b0481edbe733516c473689c5f1439ce0f2246de88"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}